package com.qls.ali.biz.data.controller;


import com.qls.ali.biz.service.fz.RpService;
import com.qls.core.constant.Constant;
import com.qls.core.dto.PushRpBody;
import com.qls.core.entity.DtStore;
import com.qls.core.utils.R;
import com.qls.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;


@Slf4j
@RestController
@RequestMapping("rp")
public class RpController {
    @Autowired
    private RpService rpService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("push")
    public R push(@RequestBody PushRpBody pushRpBody) {
        Integer storeId = pushRpBody.getStoreId();
        DtStore dtStore = (DtStore) redisUtil.hashGet(Constant.Redis.STOREID_USER_STORE.getValue(), storeId.toString());
        if (Objects.isNull(dtStore)) {
            log.warn("rp推送，店铺id：{}，未设置缓存", storeId);
            return R.error("rp推送，店铺未设置缓存");
        }
        String appKey = dtStore.getAppKey();
        String session = dtStore.getSessionKey();
        String secret = dtStore.getSecret();
        Integer type = pushRpBody.getType();
        Long rpCode = pushRpBody.getRpCode();
        Integer partnerId = pushRpBody.getPartnerId();
        List<Integer> crpIds = pushRpBody.getCrpIds();
        switch (type) {
            case -1:
                rpService.del(rpCode, crpIds, storeId, appKey, secret, session);
                break;
            case 1:
                rpService.push(rpCode, crpIds, storeId, appKey, secret, session, partnerId);
                break;
            default:
                log.warn("酒店推送任务type错误");
                break;
        }
        return R.ok();
    }


}
